package cn.com.platform.web.manage.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.easy.dto.MessageDTO;
import cn.com.easy.permission.service.system.urlreserve.anotion.UrlReserveAnotion;
import cn.com.easy.permission.web.dto.PageRequestParamDTO;
import cn.com.easy.utils.DateUtils;
import cn.com.easy.utils.ExcelPoiUtil;
import cn.com.easy.utils.ExcelPoiUtil.CellDto;
import cn.com.easy.utils.PageUtils;
import cn.com.easy.utils.ResponseOutputUtils;
import cn.com.platform.entity.MemberEntity;
import cn.com.platform.mybatis.MemberMybatisDao;
import cn.com.platform.web.manage.dto.MemberDto;
import cn.com.platform.web.manage.dto.MemberQueryParamDto;

import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.google.common.collect.Lists;

/**
 * 用户管理控制器
 * 
 * @author xinjie 2017年6月19日
 * 
 */
@Controller
@RequestMapping("/manage/member")
public class MemberManageController {
	private Logger logger = LoggerFactory.getLogger(MemberManageController.class);

	/** 用户管理MybatisDao */
	@Autowired
	private MemberMybatisDao memberMybatisDao;

	/**
	 * 分享类别管理首页
	 * 
	 * @param req
	 * @param res
	 * @return
	 * @auth xinjie 2017年6月19日
	 */
	@UrlReserveAnotion("用户管理首页")
	@RequestMapping("")
	public String index(HttpServletRequest req, HttpServletResponse res) {
		return "/manage/member/member";
	}

	/**
	 * 分享搜索
	 * 
	 * @param pageRequestParamDTO
	 * @param req
	 * @param res
	 * @throws Exception
	 * @author xinjie 2017年6月19日
	 */
	@UrlReserveAnotion("用户搜索")
	@RequestMapping("/search.json")
	public void search(PageRequestParamDTO pageRequestParamDTO, MemberQueryParamDto memberQueryParamDto, HttpServletRequest req, HttpServletResponse res) throws Exception {
		try {
			PageList<MemberEntity> memberEntity = null;
			if (StringUtils.isBlank(pageRequestParamDTO.getSortName()) == true) {
				pageRequestParamDTO.setSortName("createTime");
				pageRequestParamDTO.setSortOrder("desc");
			}
			memberEntity = memberMybatisDao.findAll(memberQueryParamDto, pageRequestParamDTO.buildMyBatisPageRequest());
			cn.com.easy.utils.Page<MemberEntity> page = PageUtils.getPage(memberEntity);
			ResponseOutputUtils.renderJson(res, MessageDTO.newInstance("", true, page));
		} catch (Exception e) {
			logger.error("用户搜索错误：", e);
			ResponseOutputUtils.renderJson(res, MessageDTO.newInstance("", false, "用户搜索错误"));
		}
	}

	/**
	 * 导出EXCEL
	 * 
	 * @param pageRequestParamDTO
	 * @param memberQueryParamDto
	 * @param req
	 * @param res
	 * @throws Exception
	 * @auth xinjie 2017年7月17日
	 */
	@UrlReserveAnotion("导出EXCEL")
	@RequestMapping("/excel.json")
	public void saveExcel(PageRequestParamDTO pageRequestParamDTO, MemberQueryParamDto memberQueryParamDto, HttpServletRequest req, HttpServletResponse res) throws Exception {
		try {
			List<MemberEntity> memberList = memberMybatisDao.findAllExcel(memberQueryParamDto);
			List<CellDto> fieldMap = new ArrayList<CellDto>();
			CellDto cellDto1 = new CellDto();
			cellDto1.setCname("用户类型");
			cellDto1.setEnName("from");
			fieldMap.add(cellDto1);
			CellDto cellDto2 = new CellDto();
			cellDto2.setCname("昵称");
			cellDto2.setEnName("nickName");
			fieldMap.add(cellDto2);
			CellDto cellDto3 = new CellDto();
			cellDto3.setCname("真实名称");
			cellDto3.setEnName("realname");
			fieldMap.add(cellDto3);
			CellDto cellDto4 = new CellDto();
			cellDto4.setCname("手机");
			cellDto4.setEnName("mobile");
			fieldMap.add(cellDto4);
			CellDto cellDto5 = new CellDto();
			cellDto5.setCname("邮箱");
			cellDto5.setEnName("mail");
			fieldMap.add(cellDto5);
			CellDto cellDto6 = new CellDto();
			cellDto6.setCname("创建时间");
			cellDto6.setEnName("createTimeString");
			fieldMap.add(cellDto6);
			if (CollectionUtils.isNotEmpty(memberList) == true) {
				// 设置response头信息
				// 设置默认文件名为当前时间：年月日时分秒
				String fileName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()).toString();
				res.reset();
				res.setContentType("application/vnd.ms-excel"); // 改成输出excel文件
				res.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xls");

				List<MemberDto> memberDtoList = Lists.newArrayList();

				for (MemberEntity memberEntity : memberList) {
					{
						MemberDto memberDto = new MemberDto();
						BeanUtils.copyProperties(memberEntity, memberDto);
						memberDto.setCreateTimeString(DateUtils.formatDate(memberEntity.getCreateTime(), "yyyy-MM-dd hh:mm:ss"));
						memberDtoList.add(memberDto);
					}

				}

				ExcelPoiUtil.listToExcel(memberDtoList, fieldMap, "用户", 65535, null, "", 1, true, res.getOutputStream());
			} else {
				ResponseOutputUtils.renderJson(res, MessageDTO.newInstance("", false, "没有数据"));
			}
		} catch (Exception e) {
			logger.error("导出EXCEL错误：", e);
			// ResponseOutputUtils.renderJson(res, MessageDTO.newInstance("",
			// false, "导出EXCEL错误"));
		}
	}

}
